//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.thingsboard.server.config.oauth2;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import org.springframework.core.convert.converter.Converter;
import org.springframework.security.oauth2.core.OAuth2AccessToken;
import org.springframework.security.oauth2.core.OAuth2AccessToken.TokenType;
import org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse;
import org.springframework.util.StringUtils;

public final class DefaultMapOAuth2AccessTokenResponseConverter implements Converter<Map<String, Object>, OAuth2AccessTokenResponse> {
    private static final Set<String> TOKEN_RESPONSE_PARAMETER_NAMES = new HashSet(Arrays.asList("access_token", "expires_in", "refresh_token", "scope", "token_type"));

    public DefaultMapOAuth2AccessTokenResponseConverter() {
    }

    public OAuth2AccessTokenResponse convert(Map<String, Object> source) {
        String accessToken = getParameterValue(source, "access_token");
        OAuth2AccessToken.TokenType accessTokenType = getAccessTokenType(source);
        long expiresIn = getExpiresIn(source);
        Set<String> scopes = getScopes(source);
        String refreshToken = getParameterValue(source, "refresh_token");
        Map<String, Object> additionalParameters = new LinkedHashMap();

        for(Map.Entry<String, Object> entry : source.entrySet()) {
            if (!TOKEN_RESPONSE_PARAMETER_NAMES.contains(entry.getKey())) {
                additionalParameters.put((String)entry.getKey(), entry.getValue());
            }
        }

        return OAuth2AccessTokenResponse.withToken(accessToken).tokenType(accessTokenType).expiresIn(expiresIn).scopes(scopes).refreshToken(refreshToken).additionalParameters(additionalParameters).build();
    }

    private static OAuth2AccessToken.TokenType getAccessTokenType(Map<String, Object> tokenResponseParameters) {
        return TokenType.BEARER.getValue().equalsIgnoreCase(getParameterValue(tokenResponseParameters, "token_type")) ? TokenType.BEARER : null;
    }

    private static long getExpiresIn(Map<String, Object> tokenResponseParameters) {
        return getParameterValue(tokenResponseParameters, "expires_in", 0L);
    }

    private static Set<String> getScopes(Map<String, Object> tokenResponseParameters) {
        if (tokenResponseParameters.containsKey("scope")) {
            String scope = getParameterValue(tokenResponseParameters, "scope");
            return new HashSet(Arrays.asList(StringUtils.delimitedListToStringArray(scope, " ")));
        } else {
            return Collections.emptySet();
        }
    }

    private static String getParameterValue(Map<String, Object> tokenResponseParameters, String parameterName) {
        Object obj = tokenResponseParameters.get(parameterName);
        return obj != null ? obj.toString() : null;
    }

    private static long getParameterValue(Map<String, Object> tokenResponseParameters, String parameterName, long defaultValue) {
        long parameterValue = defaultValue;
        Object obj = tokenResponseParameters.get(parameterName);
        if (obj != null) {
            if (obj.getClass() == Long.class) {
                parameterValue = (Long)obj;
            } else if (obj.getClass() == Integer.class) {
                parameterValue = (long)(Integer)obj;
            } else {
                try {
                    parameterValue = Long.parseLong(obj.toString());
                } catch (NumberFormatException var8) {
                }
            }
        }

        return parameterValue;
    }
}
